package com.example.happiness.controller.employee;

import com.example.happiness.constant.Message;
import com.example.happiness.pojo.Employee;
import com.example.happiness.pojo.Order;
import com.example.happiness.pojo.Serve;
import com.example.happiness.pojo.employeevo.EmployOrderVO;
import com.example.happiness.pojo.employeevo.EmployeeRecordVO;
import com.example.happiness.pojo.employeevo.EmployeeTimeLongVO;
import com.example.happiness.pojo.generalDTO.OrderStateDTO;
import com.example.happiness.result.Result;
import com.example.happiness.service.employee.EmployeeOrderService;
import com.example.happiness.service.employee.EmployeeServeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Api(tags = "派单相关接口")
@Slf4j
@RestController
@RequestMapping("/employee/order")
public class EmployOrderController {

    @Autowired
    EmployeeOrderService employeeOrderService;
    @Autowired
    EmployeeServeService employeeServeService;

    @GetMapping("/getemployOD")
    @ApiOperation("员工获取派单")
    public Result<List<EmployOrderVO>> getEmployOD(String eid) {
        List<EmployOrderVO> employOrderVOS = employeeOrderService.getEmployOD(eid);
        return Result.success(employOrderVOS);
    }

    @PutMapping("/updataOrderState")
    @ApiOperation("员工修改订单状态")
    public Result updataOrderState(@RequestBody OrderStateDTO orderStateDTO){
        Order order = employeeOrderService.getOrder(orderStateDTO.getOrderid());
        Duration duration = Duration.between(LocalDateTime.now(), order.getOrderdate());
        long hours = duration.toHours();
        if(Objects.equals(orderStateDTO.getState(), Message.FOUR)){
            if(hours > 3){
                return Result.error("时间未到");
            }
        }
        if(Objects.equals(orderStateDTO.getState(), Message.NINE)){
            if(hours > 0){
                return Result.error("服务时间未结束");
            }
        }
        employeeOrderService.updataOrderState(orderStateDTO);
        return Result.success();
    }

    @GetMapping("/getorderServe")
    @ApiOperation("员工获取服务记录")
    public Result<List<EmployeeRecordVO>> getServe(String eid){
        List<EmployeeRecordVO> employeeRecordVOS = employeeOrderService.getServe(eid);
        return Result.success(employeeRecordVOS);
    }

    @GetMapping("/getorderTime")
    @ApiOperation("员工获取当日和当月订单数")
    public Result<EmployeeTimeLongVO> getOrder(String eid){
        EmployeeTimeLongVO employeeTimeLongVO = employeeOrderService.getTimeLong(eid);
        return Result.success(employeeTimeLongVO);
    }
}
